package main;
import voip.Session;
import voip.SIPProperty;
import http.MultiThreadedServer;

public class SIPSpeaker
{
	public static SIPProperty sipProperty;
	public String messageRecord = "";
	static String filename="";
	static String sip_uri="";
	static String http_bind_address="";
	static String http_port = "80";
	
	public static void getArgument(String filename, String sip_uri, String http_addr)
	{
		sipProperty = new SIPProperty(filename, sip_uri, http_addr);		
		sipProperty.run(filename);
	}

	public static void main(String[] args)
	{
		int i = 0;

		if(args.length == 0)
		{
			Thread obSipStart = new Thread(){
				public void run() {
					Session sip_session = new Session();
					sip_session.run();
				}
			 };
			 obSipStart.start();
		}
		else if(args.length > 0)
		{
			// Read configuration file.
			while (i < args.length) 
			{
				// For debugging
				//System.out.println("args[" +i + "] :" + args[i]);
				if (args[i].equals("-c")) 
				{
					if(i+1 == args.length)
					{
						System.out.println("[Error] No input filename.");
						System.exit(0);
					}
					else
						filename = args[i + 1];				           
	            }
				
				else if(args[i].equals("-user"))
				{
					// sip_uri
					if(i+1 == args.length)
					{
						System.out.println("[Error] No user infomation.");
						System.exit(0);					
					}
					else
						sip_uri = args[i+1];
				}
				else if(args[i].equals("-http"))
				{
					if(i+1 == args.length)
					{
						System.out.println("[Error] No http bind address.");
						System.exit(0);					
					}
					else
						http_bind_address = args[i+1];
				}
				i++;
			}
			Thread obSipStart = new Thread(){
				public void run() {
					Session sip_session = new Session(filename, sip_uri, http_bind_address);
					sip_session.run();
				}
			 };
			 obSipStart.start();
		}

		int idx = http_bind_address.indexOf(":");
		if(idx != -1) {
			http_port = http_bind_address.substring(idx+1);
		}
		Thread obHttpStart = new Thread(){
			public void run() {
				MultiThreadedServer httpServer = new MultiThreadedServer(http_port);
				httpServer.run();
			}
		 };
		 obHttpStart.start();
		 
	}
}